/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.SABM;

import Beans.StakeholderAccountsBeanRemote;
import CustomerIOTRPortal.FakeMainPortalMB;
import Entity.Stakeholder;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import Web_Helper.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedProperty;

/**
 *
 * @author Randall Chia
 */
@ManagedBean (name="StakeholderPointsMB")
@SessionScoped
public class StakeholderPointsManagerBean {
    
    private List<pointPurchaseItem> purchaseList;        
    private pointPurchaseItem selectedPurchaseItem;
    private double purchasePrice;
    private Date transactionTimestamp;
    
    
    @ManagedProperty(value = "#{fakeMainPortalMB}")
    private FakeMainPortalMB portalManagedBean;
    
    @EJB
    private StakeholderAccountsBeanRemote stakeholderManager;
    

    /** Creates a new instance of StakeholderPointsManagerBean */
    public StakeholderPointsManagerBean() {
        purchaseList = new ArrayList<pointPurchaseItem>();
        purchaseList.add(new pointPurchaseItem(1000, 40.0));
        purchaseList.add(new pointPurchaseItem(5000, 180.0));
        purchaseList.add(new pointPurchaseItem(10000, 320.0));
    }
    
    
    
    
    
    
    
    
    
    //Perform validation of a payment for buying loyalty points from TCube
    public void validatePayment() {

        boolean validationResult = false;
        try {
            validationResult = Helper_PaymentValidator.validatePayment();
        } catch (IOException e) {
        }

        if (validationResult == true) {
            System.out.println("Payment Validation: SUCCESS");
            
            transactionTimestamp = new Date();
            
            stakeholderManager.givePointsToStakeholder(portalManagedBean.getStakeholderName(), selectedPurchaseItem.getQuantity());
            System.out.println("Gave " + selectedPurchaseItem.getQuantity() + " points to " + portalManagedBean.getStakeholderName());
            
            //TODO create purchase log entry
            
        }//end THEN block
        else {
            System.out.println("Payment Validation: FAIL");
        }

    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public void updatePurchasePrice(){
        purchasePrice = selectedPurchaseItem.getPrice();
    }

    /**
     * @return the purchaseList
     */
    public List<pointPurchaseItem> getPurchaseList() {
        return purchaseList;
    }

    /**
     * @param purchaseList the purchaseList to set
     */
    public void setPurchaseList(List<pointPurchaseItem> purchaseList) {
        this.purchaseList = purchaseList;
    }

    /**
     * @return the selectedPurchaseItem
     */
    public pointPurchaseItem getSelectedPurchaseItem() {
        return selectedPurchaseItem;
    }

    /**
     * @param selectedPurchaseItem the selectedPurchaseItem to set
     */
    public void setSelectedPurchaseItem(pointPurchaseItem selectedPurchaseItem) {
        this.selectedPurchaseItem = selectedPurchaseItem;
    }

    /**
     * @return the purchasePrice
     */
    public double getPurchasePrice() {
        return purchasePrice;
    }

    /**
     * @param purchasePrice the purchasePrice to set
     */
    public void setPurchasePrice(double purchasePrice) {
        this.purchasePrice = purchasePrice;
    }

    /**
     * @return the portalManagedBean
     */
    public FakeMainPortalMB getPortalManagedBean() {
        return portalManagedBean;
    }

    /**
     * @param portalManagedBean the portalManagedBean to set
     */
    public void setPortalManagedBean(FakeMainPortalMB portalManagedBean) {
        this.portalManagedBean = portalManagedBean;
    }

    /**
     * @return the transactionTimestamp
     */
    public Date getTransactionTimestamp() {
        return transactionTimestamp;
    }

    /**
     * @param transactionTimestamp the transactionTimestamp to set
     */
    public void setTransactionTimestamp(Date transactionTimestamp) {
        this.transactionTimestamp = transactionTimestamp;
    }
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
